//
// Camera.cpp for bomberman in /home/lefebv_w//workspace/cpp/bomberman/tests/compil
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Thu May  2 18:33:21 2013 damien lefebvre
// Last update Wed May  8 21:00:43 2013 damien lefebvre
//

#include	"Camera.hh"

Camera::Camera(void)
  : m_t(0.0f), m_r(1000.0f), m_position(0.0f, 1000.0f, m_r), m_rotation(0.0f, 0.0f, 0.0f)
{}

void	Camera::initialize(void)
{
  std::cout << "Camera control : press Q and S to control angle, A and Z ton control radius, E and D ton control height." << std::endl;

  // config of camera's frustum
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(70.0f, 800.0f/600.0f, 1.0f, 10000.0f);
  gluLookAt(m_position.x, m_position.y, m_position.z,
	    0.0f, 500.0f, 0.0f,
	    0.0f, 1.0f, 0.0f);

  // set position of camera
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  // activate deep test
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
}

void	Camera::update(gdl::GameClock const & gameClock, gdl::Input & input)
{
  bool	update = false;

  if (input.isKeyDown(gdl::Keys::Q) == true)
    {
      m_t = m_t + 0.05f;
      update = true;
    }
  if (input.isKeyDown(gdl::Keys::S) == true)
    {
      m_t = m_t - 0.05f;
      update = true;
    }
  if (input.isKeyDown(gdl::Keys::A) == true)
    {
      m_r = m_r + 10.0f;
      update = true;
    }
  if (input.isKeyDown(gdl::Keys::Z) == true)
    {
      m_r = m_r - 10.0f;
      update = true;
    }
  if (input.isKeyDown(gdl::Keys::E) == true)
    {
      m_position.y = m_position.y + 10.0f;
      update = true;
    }
  if (input.isKeyDown(gdl::Keys::D) == true)
    {
      m_position.y = m_position.y - 10.0f;
      update = true;
    }
  
  if (update == true)
    {
      m_position.z = m_r * cos(m_t);
      m_position.x = m_r * sin(m_t);
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(70.0f, 800.0f/600.0f, 1.0f, 10000.0f);
      gluLookAt(m_position.x, m_position.y, m_position.z,
		0.0f, 500.0f, 0.0f,
		0.0f, 1.0f, 0.0f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }
}
